﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using ZFSData;
using ZFSDomain.Common.CoreLib;
using ZFSDomain.Common.CoreLib.Helper;
using ZFSDomain.Interface.User;
using ZFSDomain.Service;
using ZFSDomain.SysModule;
using ZFSInterface.User;

namespace ZFSDomain.ViewModel.User
{
    public class UserViewModel : BaseOperation
    {
        /// <summary>
        /// 初始化
        /// </summary>
        public override void InitViewModel()
        {
            UserModelList = new ObservableCollection<tb_User>();
            base.InitViewModel();
        }

        #region 属性


        private ObservableCollection<tb_User> userModelList;

        /// <summary>
        /// 用户数据集
        /// </summary>
        public ObservableCollection<tb_User> UserModelList
        {
            get { return userModelList; }
            set { userModelList = value; RaisePropertyChanged(); }
        }

        #endregion
        
        public override void GetPageData(int pageIndex)
        {
            try
            {
                IUser user = ZFSBridge.BridgeFactory.GetUserBridge();
                var UserList = user.GetUsers(SearchText, PageSize, pageIndex, out int total);
                TotalCount = total;
                UserModelList.Clear();
                UserList.ForEach((arg) => UserModelList.Add(arg));
                base.GetPageData(pageIndex);

            }
            catch (Exception ex)
            {
                Msg.Error(ex.Message);
            }
        }
        /// <summary>
        /// 重置
        /// </summary>
        public override void Reset()
        {
            base.Reset();
        }

        /// <summary>
        /// 新增
        /// </summary>
        public override void Add()
        {
            UserDialogViewModel model = new UserDialogViewModel();
            var dialog = ServiceProvider.Instance.Get<IUserDialog>();
            dialog.BindViewModel(model);
            var result = dialog.ShowDialog();
            if (result.HasValue && result.Value)
            {
                try
                {
                    ZFSInterface.User.IUser userSerivce = ZFSBridge.BridgeFactory.GetUserBridge();
                    bool r = userSerivce.Add(model.User);
                    if (r)
                    {
                        UserModelList.Add(model.User);
                        Msg.Info("添加成功!");
                    }
                }
                catch (Exception ex)
                {
                    Msg.Error(ex.Message);
                }
            }
        }

        /// <summary>
        /// 编辑
        /// </summary>
        public override void Edit()
        {
            if (!this.GetButtonAuth(Authority.EDIT)) return;

            if (CurrentRow != null)
            {
                UserDialogViewModel view = new UserDialogViewModel();
                view.User = DeepCopy.CopyByReflect((tb_User)this.CurrentRow);

                var dialog = ServiceProvider.Instance.Get<IUserDialog>();
                dialog.BindViewModel(view);
                var result = dialog.ShowDialog();
                if (result.HasValue && result.Value)
                {
                    ZFSInterface.User.IUser userSerivce = ZFSBridge.BridgeFactory.GetUserBridge();
                    bool r = userSerivce.Update(view.User);
                    if (r)
                    {
                        var mod = UserModelList.FirstOrDefault(t => t.isid.Equals(view.User.isid));
                        UserModelList.Remove(mod);
                        UserModelList.Add(view.User);
                        Msg.Info("更新成功!");
                    }
                }
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        public override void Del()
        {
            if (CurrentRow != null)
            {
                var model = DeepCopy.CopyByReflect((tb_User)this.CurrentRow);

                bool result = Msg.Question(string.Format("确认删除用户：{0}", model.UserName));

                try
                {
                    if (result)
                    {
                        ZFSInterface.User.IUser userSerivce = ZFSBridge.BridgeFactory.GetUserBridge();
                        bool r = userSerivce.Delete(model.Account);
                        if (r)
                        {
                            var mod = UserModelList.FirstOrDefault(t => t.isid.Equals(model.isid));
                            UserModelList.Remove(mod);
                            Msg.Info("删除成功!");
                        }
                    }
                }
                catch (Exception ex)
                {
                    Msg.Error(ex.Message);
                }
            }
        }
    }
}
